from __future__ import division    #so that / represents true division
import imagecache

#construct a Plant object from a dictionary
def Plant_from_dict(d):
    return Plant(d['name'], d['growth_time'], d['base_price'], d['tilename'])

class Plant:
    def __init__(self, name, growth_time, base_price, tilename):
        self.name = name
        self.growth_time = growth_time
        self.base_price = base_price
        self.age = 0
        self.tilename = tilename
        self.watered = False
        self.unwatered_days = 0
        
    #increment age
    def inc_age(self):
        if(self.watered):
            self.age += 1
        else:
            self.unwatered_days += 1
            if(self.unwatered_days >= 3):
                self.age = self.growth_time * 5 #make dead
        self.watered = False
    
    def set_watered(self):
        self.watered = True
    
    #calculate the price, which is always an int
    def get_price(self):
        price = 0
        #dead
        if(self.age >= self.growth_time * 2):
            price = 0
        #old
        elif(self.age > self.growth_time):
            price = self.base_price*(self.growth_time/self.age)
        #young
        elif(self.age < self.growth_time):
            price = self.base_price*(self.age/self.growth_time)
        #ripe
        else:
            price = self.base_price
        return int(price)
            
    def get_tilename(self):
        if(self.age == 0):
            return self.tilename+"-0"
        elif(self.age < self.growth_time//2):
            return self.tilename+"-1"
        elif(self.age < self.growth_time):
            return self.tilename+"-2"
        elif(self.age == self.growth_time):
            return self.tilename+"-3"
        elif(self.age > self.growth_time * 2):
            return self.tilename+"-5"
        elif(self.age > self.growth_time):
            return self.tilename+"-4"
            
    def get_image(self):
        return imagecache.get_image(self.get_tilename())
        
    def __str__(self):
        return self.name[0]